<?php

namespace app\home\controller;

use think\Controller;
use think\Request;

class Order extends Base
{

    public function create()
    {
        //
        if(!session('?user_info')){
            //设置登陆成功后的跳转地址
            session('back_url','home/cart/index');
            $this->redirect('home/login/login');
        }


        $user_id = session('user_info.id');
        $address = \app\common\model\Address::where('user_id', $user_id)->select();
        $cart_data=\app\home\model\Goods::alias('t1')->join('Cart t2','t1.`id`=t2.`goods_id`','left')
        ->field('t1.*,t2.id,t2.user_id,t2.number,t2.is_selected')->where('t2.user_id',$user_id)->select();
        // $cart_data = \app\common\model\Cart::with('goods')->where('is_selected', 1)->where('user_id', $user_id)->select();
        // $cart_data = (new \think\Collection($cart_data))->toArray();
        // dump($address);die;
        // dump($cart_data);die;
        // $res = $this::getcarData();
        
        //        $res['address'] = $address;
        // //        return view('create', $res);
        //         $cart_data = $res['cart_data'];
                // $total_number = $res['total_number'];
                // $total_price = $res['total_price'];
        
                return view('create', [ 'address'=>$address,'cart_data'=>$cart_data]);
        
    }
    // public static function getcarData()
    // {
    //     $user_id=session('user_id.id');
    //     $cart_data=\app\common\model\Cart::with('goods')->where('is_selected',1)->where('user_id',$user_id)->select();
    //     $totsl_price=0;
    //     $totsl_number=0;
    //     foreach($cart_data as $v){
    //         // $total_number +=$v ['number'];
    //         // $total_price += $v['price'] * $v['number'];


    //     }

    //     return[
    //         'cart_data'=>$cart_data,
    //         // 'total_number'=>$total_number,
    //         // 'total_price'=>$total_price

    //     ];
    // }

    public function save(Request $request)
    {
        $params=input();
        // dump($params);
        // dump($params);die;
        $validate=$this->validate($params,[
            // 'address_id'=>'require|integer|gt:0',
        ]);
        if($validate !==true){
            $this->error($validate);
        }

        $address=\app\common\model\Address::find($params,['addresss_id']);
        $order_sn=time().mt_rand(100000,999999);
        $user_id=session('user_info.id');

        $res=\app\home\model\Goods::alias('t1')->join('Cart t2','t1.`id`=t2.`goods_id`','left')
        ->field('t1.*,t2.id,t2.user_id,t2.number,t2.is_selected')->where('t2.user_id',$user_id)->select();
        
        $order_data=[
            'user_id'=>$user_id,
            'order_sn'=>$order_sn,
            'consigne'=>$address['consignee'],
            'address'=>$address['area'].$address['address'],
            'phone'=>$address['phone'],
            
        ];

        \think\Db::startTrans();
        $cart_data=\app\home\model\Goods::alias('t1')->join('Cart t2','t1.`id`=t2.`goods_id`','left')
        ->field('t1.*,t2.goods_id,t2.id,t2.user_id,t2.number,t2.is_selected')->where('t2.user_id',$user_id)->select();
    //    dump($cart_data);
        // try{
            // foreach($res['cart_data'] as $v){
            //     if($v['number']>$v['goods_number']){
            //         //抛出异常
            //         throw new \Exception('订单包含库存不足的商品');

            //     }
            // }
            $order = \app\common\model\Order::create($order_data);
            //向订单商品表添加多条数据
            $order_goods_data = [];
            foreach( $cart_data as $v){
                $row = [
                    'order_id' => $order['id'],
                    'goods_id' => $v['goods_id'],
                    
                    'number' => $v['number'],
                    'goods_name' => $v['goods_name'],
                    'goods_logo' => $v['goods_logo'],
                    'goods_price' => $v['goods_price'],
                   
                ];
                $order_goods_data[] = $row;
            }
            //批量添加
            $model = new \app\common\model\OrderGoods();
            $model->saveAll($order_goods_data);
            //从购物车表删除对应数据
            //\app\common\model\Cart::where(['user_id' => $user_id, 'is_selected'=>1])->delete();
            //\app\common\model\Cart::where('user_id',$user_id)->where('is_selected',1)->delete();
            //提交事务
           
            \think\Db::commit();
            //展示选择支付方式页面 。。。
            $pay_type = config('pay_type');
            // dump($pay_type);
            return view('pay', ['order_sn' => $order_sn, 'pay_type'=>$pay_type]);
        // }catch (\Exception $e){
        //     回滚事务
        //     \think\Db::rollback();
        //     $this->error('创建订单失败，请重试');
        // }


    }

    //页面跳转 同步通知地址
    public function callback(){
        $params = input();
        dump($params);die;
        return view();

    }
    public function paysuccess(){
        return view('paysuccess');

    }



}
